--[[ New Pause Menu ]] --
-- by EeveeEuphoria

-- CREATED FOR INTERNAL USE IN THIS EPISODE!


--[[ GOALS:
    - Create a pause menu system that looks identical to vanilla SMBX's pause menu, but of course have it be customizable.
    - Use something like pausemenu.addOption to add options to the menu, and state the function it'll do.
    - Make a sound for when the player saves (ala super mario all-stars)
    - Would be neat to include that clock concept I've been thinking of in this.

    - could try using SFX 59 (dragon coin) for confirmation noises

]]

--[[ TODO:
    - Gray out the return to hub world option if player is in mid-air
        - plyr:isOnGround()

]]

local newPauseMenu = {}


function newPauseMenu.onInitAPI()
	registerEvent(newPauseMenu, "onTick")
	registerEvent(newPauseMenu, "onStart")
	registerEvent(newPauseMenu, "onDraw")
	registerEvent(newPauseMenu, "onPause")
	registerEvent(newPauseMenu, "onInputUpdate")
end

-- player.rawKeys.pause

local pauseTime = false
local page = 1
local position = 1
local cursorpos = 0
local fakecursorpos = cursorpos
local spacing = 40
local yoffset = -14
local opacity = 0

local menuNames = {
    [1] = "Continue",
    [2] = "Restart",
    [3] = "Exit to Hub World",
}

local function unpause(plyr)
    Misc.unpause()
    pauseTime = false
    plyr:mem(0x11E,FIELD_BOOL,false)
end

local function restart(plyr)
    unpause(plyr)
    mem(0x00B2C6DA, FIELD_WORD, -1);
    mem(0x00B25720, FIELD_STRING, Level.filename());
    mem(0x00B250B4, FIELD_WORD, 0);
    mem(0x00B25134, FIELD_WORD, 0);
    mem(0x00B2C89C, FIELD_WORD, 0);
    mem(0x00B2C620, FIELD_WORD, 0);
    mem(0x00B2C5B4, FIELD_WORD, -1);
end

function newPauseMenu.onPause(eventToken, pausingPlayer)
    eventToken.cancelled = true
end

local logoGFX

function newPauseMenu.onStart()
    if Level.name() == "Mario Castle" then
       menuNames = {
            [1] = "Continue",
            [2] = "Save & Continue",
            [3] = "Save & Exit",
            [4] = "Exit",
        }
        spacing = 30
        yoffset = -4
    end
    local WorldNumber = 0
    if SearchLevelName(Level.filename()) then
        WorldNumber = SearchLevelName(Level.filename()).world
    end
    logoGFX = Graphics.loadImage(Misc.resolveFile("GFX/unusuality logo " .. WorldNumber .. ".png"))
end

local playerincontrol = nil
local cooldown = 0

function newPauseMenu.onInputUpdate()
    for _,plyr in ipairs(Player.get()) do
        if plyr.rawKeys.pause == KEYS_PRESSED and plyr.deathTimer == 0 and not mem(0x00B250E2, FIELD_BOOL) then
            pauseTime = not pauseTime
            page = 1
            position = 1
            cooldown = 2
            cursorpos = 220+(spacing*position)
            fakecursorpos = cursorpos
            if pauseTime then
                if Misc.isPaused() then
                    Misc.unpause()
                else
                    Misc.pause()
                end
            else
                Misc.unpause()
            end
            playerincontrol = plyr
            SFX.play(30)
        end
    end

    if pauseTime and playerincontrol then
        if cooldown > 0 then 
            cooldown = cooldown - 1 
        end
        if playerincontrol.rawKeys.down == KEYS_PRESSED then
            position = position + 1
            if position > table.getn(menuNames) then
                position = 1
            end
            SFX.play(71)
        elseif playerincontrol.rawKeys.up == KEYS_PRESSED then
            position = position - 1
            if position < 1 then
                position = table.getn(menuNames)
            end
            SFX.play(71)
        elseif playerincontrol.rawKeys.jump == KEYS_PRESSED or (playerincontrol.rawKeys.pause == KEYS_PRESSED and cooldown == 0) then -- choose option
            local OptionChosen = menuNames[position]
            if OptionChosen == "Continue" then
                unpause(playerincontrol)
            elseif OptionChosen == "Restart" then
                restart(playerincontrol)
            elseif OptionChosen == "Exit" then
                Misc.exitGame()
            elseif OptionChosen == "Exit to Hub World" then
                if playerincontrol:isOnGround() then
                    local WarpTime = mem(0x00B2C6D8,FIELD_WORD) or 0
                    Level.load("Mario Castle.lvlx", nil, WarpTime)
                else
                    SFX.play(39)
                    return
                end
            elseif OptionChosen == "Save & Continue" then
                Misc.saveGame()
            elseif OptionChosen == "Save & Exit" then
                Misc.saveGame()
                Misc.exitGame()
            end
            unpause(playerincontrol)
            SFX.play(59)
        end
    else
        playerincontrol = nil
    end
end

local pauseGFX = Graphics.loadImage(Misc.resolveFile("GFX/pause.png"))
local cursorGFX = Graphics.sprites.hardcoded["34-0"].img
local HardModeGFX = Graphics.loadImage(Misc.resolveFile("GFX/hard mode incomplete.png"))
local HardModeGFX2 = Graphics.loadImage(Misc.resolveFile("GFX/hard mode.png"))
--cursorGFX = Graphics.loadImage(Misc.resolveFile("GFX/player2pause.png"))

function newPauseMenu.onDraw()
    if pauseTime then
        if opacity < 1 then
            opacity = opacity + 0.1
        end
    else
        if opacity > 0 then
            opacity = opacity - 0.2
        end
    end
    Graphics.drawImageWP(pauseGFX, 210, 152, opacity, 5)
    Graphics.drawImageWP(logoGFX, 226, 156, opacity, 5)
    for key, value in pairs(menuNames) do
        local colortime = Color.white
        if position == key then
            if playerincontrol == player2 then
                colortime = Color.fromHexRGB(0x7FC3FF)
            else
                colortime = Color.fromHexRGB(0xFF7F7F)
            end
        end
        if playerincontrol and not playerincontrol:isOnGround() and value == "Exit to Hub World" then
            colortime = Color.fromHexRGB(0xBCBCBC)
        end
        textplus.print{
            x = 265,
            y = 220+(spacing*key) + yoffset,
            --pivot = {0.5, 0.5},
            text = "<size 2>" .. value .. "</size>",
            font = GoodFont,
            color = colortime*opacity,
            priority = 5
        }
    end
    cursorpos = 220+(spacing*position)
    if fakecursorpos > cursorpos then
        fakecursorpos = math.max(cursorpos, fakecursorpos - 6)
    elseif fakecursorpos < cursorpos then
        fakecursorpos = math.min(cursorpos, fakecursorpos + 6)
    end
    if playerincontrol == player2 then
        cursorGFX = Graphics.loadImage(Misc.resolveFile("GFX/player2pause.png"))
    else
        cursorGFX = Graphics.sprites.hardcoded["34-0"].img
    end
    Graphics.drawImageWP(cursorGFX, 245, fakecursorpos + yoffset, opacity, 5)

    if globalsave.HardMode then
		local Gamer = HardModeGFX
		if globalsave.HMLevelsBeat[Level.filename()] then
			Gamer = HardModeGFX2
		end
		Graphics.drawImageWP(Gamer, 550, 356, opacity, 5)
	end
end

return newPauseMenu